package com.budwk.app.waterquality.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.budwk.app.waterquality.models.Waterquality_app;
import com.budwk.app.waterquality.services.WaterqualityAppService;
import com.budwk.starter.common.openapi.annotation.*;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import javax.servlet.http.HttpServletRequest;

@IocBean
@At("/waterquality/admin/app")
@ApiDefinition(tag = "Waterquality_app")
@SLog(tag = "Waterquality_app")
@Slf4j
public class WaterqualityAppController{
    @Inject
    private WaterqualityAppService waterqualityAppService;

    @At
    @Ok("json")
    @POST
    @SLog(value = "分页查询Waterquality_app")
    @ApiOperation(name = "分页查询Waterquality_app")
    @ApiFormParams(
        value = {
            @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
            @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
            @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
            @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
        },
        implementation = Waterquality_app.class
    )
    @ApiResponses(
    implementation = Waterquality_app.class
    )
    @SaCheckPermission("waterquality.app")
    public Result<?> list(@Param("..") Waterquality_app bean, @Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }

        if (null != bean.getName() && Strings.isNotBlank(bean.getName().toString())) {
            cnd.and("name","=",bean.getName());
        }

        if (null != bean.getPath() && Strings.isNotBlank(bean.getPath().toString())) {
            cnd.and("path","=",bean.getPath());
        }

        if (null != bean.getIcon() && Strings.isNotBlank(bean.getIcon().toString())) {
            cnd.and("icon","=",bean.getIcon());
        }

        return Result.data(waterqualityAppService.listPage(pageNo, pageSize, cnd));
    }

    @At
    @Ok("json")
    @POST
    @SLog(value = "新增Waterquality_app")
    @ApiOperation(name = "新增Waterquality_app")
    @ApiFormParams(
        implementation = Waterquality_app.class
    )
    @ApiResponses
    @SaCheckPermission("waterquality.app.update")
    public Result<?> create(@Param("..")Waterquality_app waterqualityApp, HttpServletRequest req) {
        waterqualityApp.setCreatedBy(SecurityUtil.getUserId());
        waterqualityAppService.insert(waterqualityApp);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @SLog(value = "修改Waterquality_app")
    @ApiOperation(name = "修改Waterquality_app")
    @ApiFormParams(
        implementation = Waterquality_app.class
    )
    @ApiResponses
    @SaCheckPermission("waterquality.app.update")
    public Result<?> update(@Param("..")Waterquality_app waterqualityApp, HttpServletRequest req) {
        waterqualityApp.setUpdatedBy(SecurityUtil.getUserId());
        waterqualityAppService.updateIgnoreNull(waterqualityApp);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @SLog(value = "获取Waterquality_app")
    @ApiOperation(name = "获取Waterquality_app")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SaCheckPermission("waterquality.app")
    public Result<?> getData(@Param("id")String id, HttpServletRequest req) {
        Waterquality_app waterqualityApp = waterqualityAppService.fetch(id);
        if (waterqualityApp == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(waterqualityApp);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @SLog(value = "删除Waterquality_app")
    @ApiOperation(name = "删除Waterquality_app")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SaCheckPermission("waterquality.app.update")
    public Result<?> delete(@Param("id")String id, HttpServletRequest req) {
        Waterquality_app waterqualityApp = waterqualityAppService.fetch(id);
        if (waterqualityApp == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        waterqualityAppService.delete(id);
        req.setAttribute("_slog_msg", waterqualityApp.getId());
        return Result.success();
    }
}
